package com.taotao.manage.controller.api;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.taotao.commen.ItemCatResult;
import com.taotao.manage.service.ItemCatService;

/**
 * 
 * @author DuBo
 *
 */
@Controller
@RequestMapping("api/item/cat")
public class ApiItemCatController {
	
	@Autowired
	private ItemCatService itemCatService;

	private static final ObjectMapper MAPPER = new ObjectMapper();
	/**
	 * 提供前台数据接口，获取类目
	 * {data:[{u,n,i[{u,n,i[string]}]},{}]}
	 * 
	 * jsonp:乱码： 消息转换器（返回string乱码，返回对象不乱码）
	 * 
	 * @return
	 */
//	@RequestMapping("all")
//	public ResponseEntity<String> queryItemCatAll(@RequestParam(value="callback",required=false) String callBack ){
//		
//		try {
//			ItemCatResult itemCatresult = this.itemCatService.queryItemCatAllToTree();
//			String result = null;
//			if (StringUtils.isEmpty(callBack)) {
//				// 没有跨域
//				result = MAPPER.writeValueAsString(itemCatresult);
//			}else{
//				// 跨域
//				result = callBack+"("+MAPPER.writeValueAsString(itemCatresult)+")";
//				
//			}
//			 return ResponseEntity.status(HttpStatus.OK).body(result);
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
//	}
	/**
	 * 配置文件jsonp
	 * @return
	 */
	@RequestMapping("all")
	public ResponseEntity<ItemCatResult> queryItemCatAll(){
		
		try {
			ItemCatResult itemCatresult = this.itemCatService.queryItemCatAllToTree();
			 return ResponseEntity.status(HttpStatus.OK).body(itemCatresult);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
	}
	
}
